home *** CD-ROM | disk | FTP | other *** search
/ PCMania 30 / PCMania CD30.iso / pcmania / graf30 / tacs / readfld.c < prev    next >
C/C++ Source or Header  |  1988-04-27  |  2KB  |  105 lines

  1. #include <tiff.h>
  2.  
  3. extern void debug();
  4.  
  5. extern LONG lseek();
  6. extern LONG bigRead();
  7. extern LONG get_1st_ifd();
  8. extern SHORT type_length();
  9. extern BOOL access_ifd();
  10. extern BOOL read_dir();
  11.  
  12. SHORT
  13. read_fields(fhandle, fdtype, buffer, max_length)
  14. SHORT fhandle;
  15. SHORT fdtype;
  16. LONGPTR buffer;
  17. LONG max_length;
  18. {
  19.     LONG ifd_offset = get_1st_ifd(fhandle);
  20.     LONG new_offset;
  21.     TIFF_DIR_ENTRY dirent;
  22.     SHORT entry_count;
  23.     SHORT subfile_type;
  24.  
  25.     /* go through IFD's ... stop at null IFD offset */
  26.     while(ifd_offset)
  27.     {
  28.         /* get pertinent IFD data */
  29.         if(access_ifd(fhandle, ifd_offset,
  30.           (TIFF_DIR_ENTRY far *)&dirent, &entry_count, &subfile_type,
  31.           &new_offset))
  32.         {
  33.             return(0);
  34.         }
  35.  
  36.         /* match on subfile type? */
  37.         if(fdtype == subfile_type)
  38.         {
  39.             SHORT n = entry_count - 1;
  40.             /* go through each entry */
  41.             while(n-- > 0 && !read_dir(fhandle,
  42.               (TIFF_DIR_ENTRY far *)&dirent))
  43.             {
  44.                 LONG tot_len;
  45.                 LONG len;
  46. debug("    tag %d, type %d, length %ld, offset %ld\n", dirent.tag,
  47.  dirent.type, dirent.length, dirent.value_offset);
  48.                 /* get length of data to be copied */
  49.                 len = (LONG)type_length(dirent.type);
  50.                 len *= dirent.length;
  51.                 tot_len = len + SHORT_SIZE + SHORT_SIZE;
  52.  
  53.                 /* length overflow? */
  54.                 max_length -= tot_len;
  55.                 if(max_length < 0)
  56.                 {
  57.                     return(0);
  58.                 }
  59.                 *((SHORT far *)buffer) = dirent.tag;
  60.                 buffer += SHORT_SIZE;
  61.                 *((SHORT far *)buffer) = LOWORD(len);
  62.                 buffer += SHORT_SIZE;
  63.                 if(len > LONG_SIZE)
  64.                 {
  65.                     LONG cur_pos = lseek(fhandle, 0L, 1);
  66.  
  67.                     /* read data into user's buffer */
  68.                     if(lseek(fhandle, dirent.value_offset,
  69.                       0) != dirent.value_offset)
  70.                     {
  71.                         return(0);
  72.                     }
  73.                     if(bigRead(fhandle, buffer, len) !=
  74.                       len)
  75.                     {
  76.                         return(0);
  77.                     }
  78.                     buffer += len;
  79.                     lseek(fhandle, cur_pos, 0);
  80.                 }
  81.                 else
  82.                 {
  83.                     LONGPTR dp =
  84.                       (LONGPTR)&(dirent.value_offset);
  85.                     while(len-- > 0)
  86.                     {
  87.                         *buffer++ = *dp++;
  88.                     }
  89.                 }
  90.             }
  91.  
  92.             /* if we bombed out early, error exit */
  93.             if(n > 0)
  94.             {
  95.                 return(0);
  96.             }
  97.  
  98.             /* since we matched, we can now go bye-bye */
  99.             return(entry_count);
  100.         }
  101.         ifd_offset = new_offset;
  102.     }
  103.     return(0);
  104. }
  105.